#include"List.h"
//mallo一个新的结点
LT* LTBuyNode(LTDateType x)

	LT* newnode = (LT*)malloc(sizeof(LT));
	if(newnode == NULL)
	{
		perror("malloc mistake");
		exit(-1);
	}
	newnode->date = x;
	newnode->next = newnode;
	newnode->prev = newnode;
	return newnode;
}
// 初始化 & 销毁 
LT* LTInit()
{
	LT* phead = LTBuyNode(0xcccccc);
	return phead;
}
void LTDestory(LT* phead)
{
	assert(phead);
	LT* cur = phead->next;
	while(cur != phead)
	{
		LT* next = cur->next;
		free(cur);
		cur = next;
	}
	free(phead);
	phead = NULL;
}
//  增删 & 插改
void LTPushBack(LT* phead,LTDateType x)
{
	assert(phead);
	LT* newnode = LTBuyNode(x);
	//phead->prev newnode phead
	newnode->next = phead;
	newnode->prev = phead->prev;

	phead->prev->next = newnode;
	phead->prev = newnode;
}
void LTPushFront(LT* phead,LTDateType x)
{
	assert(phead);
	LT* newnode = LTBuyNode(x);

	newnode->next = phead->next;
	newnode->prev = phead;

	phead->next->prev = newnode;
	phead->next = newnode;
}
void LTPopBack(LT* phead)
{
	assert(phead && phead->next != phead);
	LT* del = phead->prev;
	//del->prev del  phead
	del->prev->next = phead;
	phead->prev = del->prev;
	free(del);
	del = NULL;
}
void LTPopFront(LT* phead)
{
	assert(phead && phead->next != phead);
	LT* del = phead->next;
	//phead del del->next
	phead->next = del->next;
	del->next->prev = phead;
	free(del);
	del = NULL;
}
LT* LTFind(LT* phead,LTDateType vimal)
{
	assert(phead);
	LT* cur = phead->next;
	while(cur != phead)
	{
		if(cur->date == vimal)
			return cur;
		cur = cur->next;
	}
	return NULL;
}
void LTModify(LT* pos,LTDateType x)
{
	assert(pos);
	pos->date = x;
}
void LTInsert(LT* pos,LTDateType x)
{
	assert(pos);
	LT* newnode = LTBuyNode(x);
	//pos newnode pos->next;
	newnode->next = pos->next;
	newnode->prev = pos;
	pos->next->prev = newnode;
	pos->next = newnode;
}
void LTDelete(LT* pos)
{
	assert(pos);
	//pos->prev pos pos->next
	pos->prev->next = pos->next;
	pos->next->prev = pos->prev;
	free(pos);
	pos = NULL;
}
//  打印链表
void LTPrint(LT* phead)
{
	assert(phead);
	LT* cur = phead->next;
	while(cur != phead)
	{
		printf("%d->",cur->date);
		cur = cur->next;
	}
	printf("phead\n");
}





